/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.is.city.webcam;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.geotools.data.DataUtilities;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.swing.JMapPane;
import org.geotools.swing.event.MapMouseAdapter;
import org.geotools.swing.event.MapMouseEvent;
import org.opengis.referencing.operation.MathTransform;
import org.geotools.referencing.operation.transform.AffineTransform2D;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.referencing.operation.TransformException;

/**
 *
 * @author oleg
 */
public class WebCamMousePaneListener extends MapMouseAdapter{
    WebCamLayer webCamLayer;
    JMapPane mapPane;

    private FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
    
    public WebCamMousePaneListener(WebCamLayer webCamLayer, JMapPane mapPane) {
        this.webCamLayer = webCamLayer;
        this.mapPane = mapPane;
       
    }


    @Override
    public void onMouseClicked(MapMouseEvent ev) {
        if (!webCamLayer.isVisible())
            return;
        try {
            super.onMouseClicked(ev);

            Envelope envelope = new Envelope(new Coordinate(ev.getX(), ev.getY()));
            envelope.expandBy(10);
            AffineTransform screenToWorldTransform = mapPane.getScreenToWorldTransform();
            MathTransform mt = new AffineTransform2D(screenToWorldTransform);
            Envelope transform = JTS.transform(envelope, mt);
            ReferencedEnvelope env = new ReferencedEnvelope(transform, mapPane.getMapContext().getCoordinateReferenceSystem());
            String geometryPropertyName = webCamLayer.getFeatureSource().getSchema().getGeometryDescriptor().getLocalName();
            Filter filter = ff.intersects(ff.property(geometryPropertyName), ff.literal(env));
            FeatureCollection features = webCamLayer.getFeatureSource().getFeatures(filter);
            SimpleFeatureCollection featureCollection = DataUtilities.collection(features);
            System.out.println("Размер массива " + features.size());
            if (features.size() > 0) {
                Iterator iterator = featureCollection.iterator();
                Object next = iterator.next();
                SimpleFeature feature = (SimpleFeature) next;
                String urlstring = feature.getProperty("URL").getValue().toString();
                URL url=new URL(urlstring);
                WebCamFrame frame=new WebCamFrame(url, 30, 320, 240);
                frame.setVisible(true);
            }
        } catch (IOException ex) {
            Logger.getLogger(WebCamMousePaneListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformException ex) {
            Logger.getLogger(WebCamMousePaneListener.class.getName()).log(Level.SEVERE, null, ex);
        }
            
        

        
    }



}
